home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / irisgl_vs_opengl / alphaTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  7.8 KB  |  342 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* alphaTest.c - shows how alpha test can be used to implement a 
  19.  *    transparency algorithm
  20.  *
  21.  *
  22.  *  Escape key              - exit the program 
  23.  *  Left Mouse Button        - change incidence and azimuth angles
  24.  *  Middle Mousebutton        - change the twist angle based on
  25.  *                  horizontal mouse movement
  26.  *  Right Mousebutton        - zoom in and out based on vertical
  27.  *                  mouse movement
  28.  *  <b> key            - cycle through different blend functions
  29.  *  <d> Key            - toggle depth mask on / off 
  30.  *  <R> key            - reset view
  31.  */
  32. #include <GL/gl.h>
  33. #include <GL/glu.h>
  34. #include <GL/glut.h>
  35. #include <math.h>
  36. #include <stdio.h>    /* for printf */
  37.  
  38. /*  Function Prototypes  */
  39.  
  40. GLvoid  initgfx( GLvoid );
  41. GLvoid  drawScene( GLvoid );
  42. GLvoid  drawObjects( GLvoid );
  43. GLvoid  reshape( GLsizei, GLsizei );
  44. GLvoid  keyboard( GLubyte, GLint, GLint );
  45. GLvoid  mouse( GLint, GLint, GLint, GLint );
  46. GLvoid  motion( GLint, GLint );
  47. GLvoid  blendFuncCycle( GLvoid );
  48.  
  49. void resetView( GLvoid );
  50. void polarView( GLfloat, GLfloat, GLfloat, GLfloat);
  51. void printHelp( char * );
  52.  
  53. /* Global Definitions */
  54.  
  55. #define KEY_ESC    27    /* ascii value for the escape key */
  56.  
  57. /* Global Variables */
  58.  
  59. static GLboolean    depthMaskFlag = GL_FALSE;
  60.  
  61. static enum        actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
  62. static GLint        action;
  63.  
  64. static GLint        xStart = 0, yStart = 0;
  65.  
  66. static GLfloat         near, far, distance, twistAngle, incAngle, azimAngle;
  67.  
  68. GLvoid
  69. main( int argc, char *argv[] )
  70. {
  71.     GLsizei width, height;
  72.  
  73.     glutInit( &argc, argv );
  74.  
  75.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  76.     height = glutGet( GLUT_SCREEN_HEIGHT );
  77.     glutInitWindowPosition( width / 4, height / 4 );
  78.     glutInitWindowSize( width / 2, height / 2 );
  79.     glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
  80.     glutCreateWindow( argv[0] );
  81.  
  82.     initgfx();
  83.  
  84.     glutMouseFunc( mouse );
  85.     glutMotionFunc( motion );
  86.     glutKeyboardFunc( keyboard );
  87.     glutReshapeFunc( reshape );
  88.     glutDisplayFunc( drawScene ); 
  89.  
  90.     printHelp( argv[0] );
  91.  
  92.     glutMainLoop();
  93. }
  94.  
  95. void
  96. printHelp( char *progname )
  97. {
  98.     fprintf(stdout, "\n%s - demonstrates how to use alpha test with\n"
  99.         "the depth buffer to implement a transparency algorithm\n\n"
  100.         "Axes: X - red, Y - green, Z - blue\n\n"
  101.         "Left Mousebutton    - move eye position\n"
  102.         "Middle Mousebutton    - change twist angle\n"
  103.         "Right Mousebutton    - move up / down to zoom in / out\n"
  104.         "<b> Key        - cycle through 3 blend functions\n"
  105.         "<d> Key        - toggle depth mask on / off\n"
  106.         "<R> Key        - reset view\n"
  107.         "Escape Key        - exit the program\n\n",
  108.         progname);
  109. }
  110.  
  111. GLvoid
  112. initgfx( GLvoid )
  113. {
  114.     GLfloat maxObjectSize = 3.0;
  115.  
  116.     glClearColor( 0.0, 0.0, 0.0, 0.0 );
  117.     glShadeModel( GL_FLAT );
  118.  
  119.     /* Set up near and far so that ( far - near ) > maxObjectSize, */
  120.     /* and determine the viewing distance (adjust for zooming) */
  121.     near = 1.0;
  122.     far = near + 8*maxObjectSize; 
  123.  
  124.     resetView();
  125.  
  126.     /* set the initial blend function */
  127.     glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  128.     fprintf(stdout, 
  129.         "glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA )\n" );
  130.  
  131.     glEnable( GL_DEPTH_TEST );
  132.     printf("depth buffer is enabled\n");
  133. }
  134.  
  135. GLvoid
  136. reshape( GLsizei width, GLsizei height )
  137. {
  138.     GLdouble            aspect;
  139.  
  140.     glViewport( 0, 0, width, height );
  141.  
  142.     aspect = (GLdouble) width / (GLdouble) height;
  143.  
  144.     glMatrixMode( GL_PROJECTION );
  145.     glLoadIdentity();
  146.     gluPerspective( 45.0, aspect, near, far );
  147.     glMatrixMode( GL_MODELVIEW );
  148. }
  149.  
  150. GLvoid  
  151. blendFuncCycle( GLvoid )
  152. {
  153.     static int whichBlendFunc = 0;
  154.     
  155.     whichBlendFunc = (whichBlendFunc + 1) % 3;
  156.     
  157.     switch (whichBlendFunc)
  158.     {
  159.     case 0:
  160.         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  161.         fprintf(stdout, 
  162.             "glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA )\n" );
  163.         break;
  164.     case 1:
  165.         glBlendFunc( GL_SRC_ALPHA, GL_ONE );
  166.         fprintf(stdout, 
  167.             "glBlendFunc( GL_SRC_ALPHA, GL_ONE )\n" );
  168.         break;
  169.     case 2:
  170.         glBlendFunc( GL_ONE, GL_ZERO );
  171.         fprintf(stdout, 
  172.             "glBlendFunc( GL_ONE, GL_ZERO )\n" );
  173.         break;
  174.     default:
  175.         break;
  176.     }
  177. }
  178.  
  179. GLvoid 
  180. keyboard( GLubyte key, GLint x, GLint y )
  181. {
  182.     switch (key) {
  183.     case 'b':    /* cycle through several blend funcs */
  184.         blendFuncCycle();
  185.         glutPostRedisplay();
  186.         break;
  187.     case 'd':
  188.         depthMaskFlag = !depthMaskFlag;
  189.         printf("writing into the depth buffer is %s\n",
  190.             (depthMaskFlag ? "disabled" : "enabled"));
  191.         glutPostRedisplay();
  192.         break;
  193.     case 'R':
  194.         resetView();
  195.         glutPostRedisplay();
  196.         break;
  197.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  198.         exit(0);
  199.     }
  200. }
  201.  
  202. GLvoid 
  203. mouse( GLint button, GLint state, GLint x, GLint y )
  204. {
  205.     if (state == GLUT_DOWN) {
  206.         switch (button) {
  207.         case GLUT_LEFT_BUTTON:
  208.             action = MOVE_EYE;
  209.             break;
  210.         case GLUT_MIDDLE_BUTTON:
  211.             action = TWIST_EYE;
  212.             break;
  213.         case GLUT_RIGHT_BUTTON:
  214.             action = ZOOM;
  215.             break;
  216.         }
  217.  
  218.         /* Update the saved mouse position */
  219.         xStart = x;
  220.         yStart = y;
  221.     } else {
  222.         action = MOVE_NONE;
  223.     }
  224.  
  225. }
  226.  
  227. GLvoid
  228. motion( GLint x, GLint y )
  229. {
  230.     switch (action) {
  231.     case MOVE_EYE:
  232.         /* Adjust the eye position based on the mouse position */
  233.         azimAngle += (GLdouble) (x - xStart);
  234.         incAngle -= (GLdouble) (y - yStart);
  235.         break;
  236.     case TWIST_EYE:
  237.         /* Adjust the eye twist based on the mouse position */
  238.         twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
  239.         break;
  240.     case ZOOM:
  241.         /* Adjust the eye distance based on the mouse position */
  242.         distance -= (GLdouble) (y - yStart)/10.0;
  243.         break;
  244.     default:
  245.         printf("unknown action %d\n", action);
  246.     }
  247.     
  248.     /* Update the stored mouse position for later use */
  249.     xStart = x;
  250.     yStart = y;
  251.  
  252.     glutPostRedisplay();
  253. }
  254.  
  255. void
  256. resetView( GLvoid )
  257. {
  258.     distance = near + (far - near) / 2.0;
  259.     twistAngle = 0.0;    /* rotation of viewing volume (camera) */
  260.     incAngle = 0.0;
  261.     azimAngle = 0.0;
  262. }
  263.  
  264. void
  265. polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
  266.             GLfloat twist)
  267. {
  268.     glTranslatef( 0.0, 0.0, -distance);
  269.     glRotatef( -twist, 0.0, 0.0, 1.0);
  270.     glRotatef( -incidence, 1.0, 0.0, 0.0);
  271.     glRotatef( -azimuth, 0.0, 0.0, 1.0);
  272. }
  273.  
  274. GLvoid
  275. drawObjects( GLvoid )
  276. {
  277.     static GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
  278.     static GLfloat green[] = { 0.0, 1.0, 0.0, 0.25 };
  279.     static GLfloat red[] = { 0.7, 0.0, 0.0, 0.75 };
  280.     
  281.     /* draw torus closest to the eye */
  282.     glPushMatrix ();
  283.         glTranslatef (0.0, 0.0, 1.0);    
  284.         glColor4fv( yellow );
  285.         glutSolidTorus(0.275, 0.85, 15, 15);
  286.     glPopMatrix ();
  287.  
  288.     /* draw cylinder further from the eye */
  289.     glPushMatrix ();
  290.         glTranslatef (0.0, 0.0, -1.0);    
  291.         glColor4fv( red );
  292.         SolidCylinder(1.0, 2.0);
  293.     glPopMatrix ();
  294.  
  295.     /* draw sphere still further from the eye */
  296.     glPushMatrix ();
  297.         glTranslatef (0.0, 0.0, -2.0);
  298.         glColor4fv( green );
  299.         glutSolidSphere(0.5, 15, 15);
  300.     glPopMatrix ();
  301. }
  302.  
  303. GLvoid
  304. drawScene( GLvoid )
  305. {
  306.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  307.  
  308.     glPushMatrix();
  309.         polarView( distance, azimAngle, incAngle, twistAngle );
  310.         XYZaxes();
  311.  
  312.         /* enable alpha testing */
  313.         glEnable( GL_ALPHA_TEST );
  314.  
  315.         /* test for alpha == 1.0, so that
  316.           * only opaque objects are drawn */
  317.         glAlphaFunc( GL_EQUAL, 1.0 );
  318.  
  319.         drawObjects();
  320.  
  321.         if ( depthMaskFlag == GL_TRUE )
  322.             glDepthMask( GL_FALSE );
  323.  
  324.  
  325.         /* test for alpha != 1.0, so that
  326.           * only transparent objects are drawn */
  327.         glAlphaFunc( GL_NOTEQUAL, 1.0 );
  328.  
  329.         glEnable( GL_BLEND );
  330.         drawObjects();
  331.         glDisable( GL_BLEND );
  332.  
  333.         if ( depthMaskFlag == GL_TRUE )
  334.             glDepthMask( GL_TRUE );
  335.  
  336.         /* disable alpha testing */
  337.         glDisable( GL_ALPHA_TEST );
  338.  
  339.     glPopMatrix();
  340.     glutSwapBuffers();
  341. }
  342.